package com.dlc.yunguang.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import butterknife.BindView;
import com.dlc.commonlibrary.widget.TitleBar;
import com.dlc.yunguang.adapter.MessageAdapter;
import com.dlc.yunguang.adapter.MsgAdapter;
import com.dlc.yunguang.base.BaseActivity;
import com.dlc.yunguang.net.bean.MsgBean;
import com.dlc.yunguang.net.exception.ErrorMsgException;
import com.dlc.yunguang.net.protocol.UserProto;
import com.dlc.yunguang.net.protocol.base.HttpProtocol;
import com.dlc.yunguang.R;
import com.holenzhou.pullrecyclerview.PullRecyclerView;
import com.holenzhou.pullrecyclerview.layoutmanager.XLinearLayoutManager;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/7/19 0019.
 */

public class MessageActivity extends BaseActivity {
    /*   @BindView(R.id.empty_view)
       TextView mEmptyView;
       @BindView(R.id.list_view)
       RecyclerView mListView;
       @BindView(R.id.swipe_refresh)
       SwipeRefreshLayout mSwipeRefresh;*/
    @BindView(R.id.msg_rv)
    PullRecyclerView msg_rv;
    @BindView(R.id.titleBar)
    TitleBar titleBar;
    private int presentPage = 1;
    private boolean isRefresh = true;
    private int pageSize = 3;
    private MessageAdapter messageAdapter;
    private MsgAdapter msgAdapter;
    private List<MsgBean.DataBean.ListBean> msgBeanList=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        titleBar.leftExit(this);
        initSwipeRefresh();
        initListView();

    }

    @Override
    protected int getLayoutID() {
        return R.layout.activity_message;
    }
    private void initSwipeRefresh() {
      /*  // 设置下拉进度的主题颜色
        mSwipeRefresh.setColorSchemeResources(R.color.colorPrimary);
        mListView.setLayoutManager(new LinearLayoutManager(this));*/

    }

    private void initListView() {
        msgAdapter=new MsgAdapter(this,R.layout.item_msg,msgBeanList);
       /* messageAdapter = new MessageAdapter(this);
        mListView.setAdapter(messageAdapter);*/
        msg_rv.setAdapter(msgAdapter);
        msg_rv.setLayoutManager(new XLinearLayoutManager(getActivity()));
        msg_rv.setColorSchemeResources(android.R.color.holo_blue_light, android.R.color.holo_red_light,
            android.R.color.holo_orange_light, android.R.color.holo_green_light); // 设置下拉刷新的旋转圆圈的颜色
        msg_rv.enablePullRefresh(true); // 开启下拉刷新，默认即为true，可不用设置
        msg_rv.enableLoadDoneTip(true, R.string.load_done_tip); // 开启数据全部加载完成时的底部提示，默认为false
        msg_rv.setOnRecyclerRefreshListener(new PullRecyclerView.OnRecyclerRefreshListener() {

            @Override
            public void onPullRefresh() {
                msg_rv.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        isRefresh = true;
                        msgBeanList.clear();
                        presentPage = 1;
                        loadListData();
                    }
                }, 1500);
            }

            @Override
            public void onLoadMore() {
                msg_rv.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        isRefresh = false;
                        presentPage++;
                        loadListData();
                    }
                }, 1500);
            }
        });
        msg_rv.postRefreshing();
    }

    @Override
    protected void onStart() {
        super.onStart();
        loadListData();
    }

    private void loadListData() {
        UserProto.get().getMsg(new HttpProtocol.Callback<MsgBean>() {
            @Override
            public void onSuccess(MsgBean msgBean) {
                Log.d("消息列表获取成功的数据---->",""+msgBean.data.list.toString());
                onGetSuccess(msgBean);
            }

            @Override
            public void onFailed(int code, ErrorMsgException e) {
                Log.d("消息列表获取失败的数据---->",e.getMessage());

            }
        });

    }
    public void onGetSuccess(MsgBean msgBean) {
        msgBeanList.clear();
        pageSize=msgBean.data.totalpage;
        for(int i=0;i< msgBean.data.list.size();i++){
            MsgBean.DataBean.ListBean dataBean= msgBean.data.list.get(i);
            msgBeanList.add(dataBean);
        }
        msgAdapter.notifyDataSetChanged();
        msgAdapter.setOnItemClickListener(new MsgAdapter.OnItemClickListener() {
            @Override
            public void onClick(int position, String id) {

              //  showLoadingDialog();
               // readMsg(id);
                startActivity(new Intent(MessageActivity.this,MessageDetailActivity.class).putExtra("id",id));

            }
        });
        if(isRefresh){
            if(msg_rv!=null){
                msg_rv.stopRefresh();
                msg_rv.enableLoadMore(pageSize > 1); // 当剩余还有大于0页的数据时，开启上拉加载更多
                if(msgBeanList.size() == 0){
                    Toast.makeText(this,getString(R.string.data_null),Toast.LENGTH_SHORT).show();
                    return;
                }
            }
        }else {
            if(msg_rv!=null){
                msg_rv.stopLoadMore();
                msg_rv.enableLoadMore(pageSize > presentPage);
            }
        }

    }

}
